//
//  YYUserAccountViewModel.swift
//  YYWeibo
//
//  Created by strongwallyi on 15/12/30.
//  Copyright © 2015年 strongwallyi. All rights reserved.
//

import UIKit

class YYUserAccountViewModel: NSObject {

    //用户模型
    var userAccount:YYUserAccount?
    
    //单例方法
    static let sharedViewModel: YYUserAccountViewModel = YYUserAccountViewModel()
    
    override init() {
        super.init()
        //解档给 userAccount 赋值
        userAccount = self.getUserAccount()
    }
    
    //用户是否登录
    var userLogin: Bool{
        //判断 accessToken 是否为空&有没有过期
        if accessToken != nil && isExpiresIn == false{
            return true
        }
        return false
    }
    
    //用户的 accessToken
    var accessToken: String? {
        return userAccount?.access_token
    }
    
    //判断账户是否过期
    var isExpiresIn: Bool{
        //获得当前账户的过期时间
        if let expiresDate = userAccount?.expiresDate{
            //现在时间
            let nowDate = NSDate()
            print(expiresDate)
            //判断当前时间和过期时间的比较
            if nowDate.compare(expiresDate) == .OrderedAscending{
                //没有过期
                return false
            }
        }
        return true
    }
    
    //归档路径
    private var archivePath = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).first! as NSString).stringByAppendingPathComponent("useraccount.archive")
    
    
    //获取 accessToken
    func loadAccessToken(code:String,compeleted:(isSuccess: Bool)->()){
        
        YYNetworkTools.sharedTools.loadAccessToken(code) { (response, error) -> () in
            if error != nil{
                print(error)
                compeleted(isSuccess: false)
                return
            }
            //判断返回值是否是字典
            guard let dict = response as? [String: AnyObject] else{
                print("返回的不是字典")
                compeleted(isSuccess: false)
                return
            }
            
            //字典转模型
            let account = YYUserAccount(dict: dict)
            print(account)
            //加载用户个人信息
            self.loadUserAccount(account, compeleted: compeleted)
        }
        
    }
    
    //如果上面的代码执行到这里,那么就说明参数一定不为 nil
    //利用 accesstoken 来获取用户信息
    func loadUserAccount(account: YYUserAccount,compeleted:(isSuccess: Bool)->()){
        
        //请求个人信息
        YYNetworkTools.sharedTools.loadUserAccount(account.access_token!, uid:account.uid!) { (response, error) -> () in
            if error != nil{
                print(error)
                compeleted(isSuccess: false)
                return
            }
            
            //取到用户的头像和昵称赋值
            guard let dict = response as? [String : AnyObject] else{
                compeleted(isSuccess: false)
                return
            }
            
            //设置头像
            account.avatar_large = dict["avatar_large"] as? String
            account.screen_name = dict["screen_name"] as? String
            
            print(account)
            //用户信息获取完成
            self.userAccount = account
            
            //保存用户登陆完成的信息
            self.saveUserAccount(account)
            //切换控制器
            compeleted(isSuccess: true)
            
        }
    }
}

//判断当前用户信息是否过期,并且内部的 accessToken 是否存在
extension YYUserAccountViewModel{

    //保存用户信息
    func saveUserAccount(account: YYUserAccount){
        print(archivePath)
        //归档
        NSKeyedArchiver.archiveRootObject(account, toFile: archivePath)
    }
    
    //解档获得的用户信息
    func getUserAccount() -> YYUserAccount? {
    
        return NSKeyedUnarchiver.unarchiveObjectWithFile(archivePath) as? YYUserAccount
    }

}




